#include "CompositeComponent.h"
#include <QDebug>

#include "UI/ModRun/Output/FactoryOutput.h"

CompositeComponent::CompositeComponent(QString strName)
{
    m_listComponent.clear();

    m_strName = strName;
}

CompositeComponent::~CompositeComponent()
{
    m_listComponent.clear();
}

void CompositeComponent::operation()
{
    QString strOutput = "CompositeComponent::operation:" + m_strName;
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    for (auto iter = m_listComponent.begin(); iter != m_listComponent.end(); iter++)
    {
        std::shared_ptr<Component> pCom = (*iter);
        if (pCom)
        {
            pCom->operation();
        }
    }
}

void CompositeComponent::Add(std::shared_ptr<Component> c)
{
    m_listComponent.push_back(c);
}

void CompositeComponent::Remove(std::shared_ptr<Component> c)
{
    auto iter = std::find(m_listComponent.begin(), m_listComponent.end(),c);
    if (iter != m_listComponent.end())
    {
        m_listComponent.erase(iter);
    }
}

std::shared_ptr<Component> CompositeComponent::GetChild(size_t index)
{
    if (index >= m_listComponent.size())
    {
        return nullptr;
    }

    return m_listComponent.at(index);
}

